import { Component, OnInit,ViewChild } from '@angular/core';
import {HttpService} from '../../../lib/service/http.service';
import {ModalDirective} from 'ngx-bootstrap';
import {adSystemApiUrl} from '../../../../config/api';

@Component({
  selector: 'client-list',
  templateUrl: 'client-list.component.html',
  styleUrls: ['client-list.component.scss'],
  providers: []
})
export class ClientListComponent implements OnInit {

  searchData: number;
  clientList = [];
  userName: string;
  psd: string;
  password_again: string;
  operateId: number;  // 当前正在操作的账号ID
  resetPsd: string;
  resetPsdAgain: string;

  type: string;
  message: string;
  isShow = true;
  totalItems: number;
  currentPage: number = 1;
  itemsPerPage: number = 12;

  @ViewChild('addClientModal') public addClientModal: ModalDirective;
  @ViewChild('resetPsdModal') public resetPsdModal: ModalDirective;
  constructor(private httpService: HttpService) { }

  ngOnInit() {
    this.getClientList(1);
  }

  //重置
  reset(){
    this.searchData = null;
    this.getClientList(1);
  }
  //搜索
  search(){
    this.getClientList(1,this.searchData);
  }

  // 获取客户列表
  getClientList(page: number,mobile?: number) {
    if(!!mobile){
      var url = `${adSystemApiUrl}/v1/admin/account/list?mobile=${mobile}&page=${page}&pageSize=${this.itemsPerPage}`;
    }else{
      var url = `${adSystemApiUrl}/v1/admin/account/list?page=${page}&pageSize=${this.itemsPerPage}`;
    }
    return this.httpService.httpGetAuth(url)
      .subscribe(
        res => {
          this.totalItems = res.data.total;
          this.clientList = res.data.data;
        }
      )
  }

  addClientSubmit() {
    if(this.psd !== this.password_again) {
      this.type = 'info';
      this.message = '两次密码输入不一致，请重新输入';
      this.isShow = true;
      return;
    }
    const url = `${adSystemApiUrl}/v1/admin/account/add`;
    let data = {name: this.userName,password: this.psd,password_again: this.password_again};
    return this.httpService.httpPostAuth(url,data)
      .subscribe(
        res => {
          if(res.code === 2000) {
            this.type = 'sucess';
            this.message = '添加成功';
            this.isShow = true;
            this.addClientModal.hide();
            this.getClientList(1);
          }else{
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      )
  }

  // 更改状态
  changStatus(id: number,status: number) {
    const url = `${adSystemApiUrl}/v1/admin/account/` + id + `/lock`;
    let changedStatus = !!status?0:1;
    return this.httpService.httpPostAuth(url,{status: changedStatus})
      .subscribe(
        res => {
          if(res.code === 2000){
            this.type = 'success';
            this.message = '修改状态成功';
            this.isShow = true;
            this.getClientList(1);
          }else{
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      )
  }

  // 重置密码
  resetPsdDialog(id: number) {
    this.operateId = id;
    this.resetPsdModal.show();
  }
  resetPsdSubmit() {
    const url = `${adSystemApiUrl}/v1/admin/account/` + this.operateId + `/reset_password`;
    let data = {password: this.resetPsd,password_again: this.resetPsdAgain};
    return this.httpService.httpPostAuth(url,data)
      .subscribe(
        res => {
          if(res.code === 2000){
            this.type = 'success';
            this.message = '密码修改成功';
            this.isShow = true;
            this.resetPsdModal.hide();
            this.getClientList(1);
          }else{
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      )
  }

  pageChanged(event: any): void {
    window.scrollTo(0,0);
    this.getClientList(event.page,this.searchData);
  }
}
